package com.hc.usb.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;

import com.hc.usb.R;
import com.hc.usb.impl.USBCommandQuery;
import com.hc.usb.impl.USBHelper;
import com.hc.usb.impl.USBConnection;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    USBConnection usbConnection = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        List<UsbDevice> usbDeviceList = new ArrayList<>();
        USBHelper.getInstance(this).findDevices(usbManager,usbDeviceList);
        for (int i = 0; i < usbDeviceList.size(); i++) {
            UsbDevice usbDevice = usbDeviceList.get(i);
            int vendorId = usbDevice.getVendorId();
            int productId = usbDevice.getProductId();
            if(vendorId == 43690 && productId == 34838){
                usbConnection = USBHelper.getInstance(this).createConnection(usbDevice);
                break;
            }
        }
        Log.d("USBINFO","usbConnection : " + usbConnection);
        if(usbConnection != null){
            if(!usbManager.hasPermission(usbConnection.getUsbDevice())){
                PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,new Intent(USBReceiver.USB_PERMISSION),0);
                IntentFilter filter = new IntentFilter(USBReceiver.USB_PERMISSION);
                USBReceiver usbReceiver = new USBReceiver();
                registerReceiver(usbReceiver,filter);
                usbManager.requestPermission(usbConnection.getUsbDevice(),pendingIntent);
                Log.d("USBINFO","requestPermission");
            }else {
                boolean open = usbConnection.open(usbManager);
                Log.d("USBINFO","open connection: " + open);
                if(open){
                    USBCommandQuery usbCommandQuery = new USBCommandQuery(100);
                    usbCommandQuery.execute(usbConnection.getUsbDeviceConnection(),usbConnection.getReadPoint(), usbConnection.getWritePoint());
                }
            }
        }
    }

    public class USBReceiver extends BroadcastReceiver{
        public static final String USB_PERMISSION = "com.hc.usb.USB_PERMISSION";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(USB_PERMISSION.equals(action)){
                if(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,false)){
                    Log.d("USBINFO","Receiver have permission");
                    UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
                    boolean open = usbConnection.open(usbManager);
                    Log.d("USBINFO","open connection: " + open);
                    if(open){
                        USBCommandQuery usbCommandQuery = new USBCommandQuery(100);
                        usbCommandQuery.execute(usbConnection.getUsbDeviceConnection(),usbConnection.getReadPoint(), usbConnection.getWritePoint());
                    }
                }else {
                    Log.d("USBINFO","Receiver not have permission");
                }
            }
        }
    }
}